package com.classic.common.simple;

import com.classic.common.MultipleStatusView;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.multimodalinput.event.TouchEvent;

import java.util.Arrays;

public class RefreshAbility extends AbsAbility implements Component.TouchEventListener {


    private ListContainer listContainer;
    private EasyListProvider mNewsAdapter;
    private VelocityDetector mVelocityTracker;
    private AnimatorProperty mAnimator, mAnimator2;
    private Image mTopImage;
    private boolean refresh;
    //记录上一次手指的X坐标
    private float mLastX = 0;
    //记录上一次手指的Y坐标
    private float mLastY = 0;
    float scrollY = 0;
    private String[] data = new String[]{"1", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2"};
    private EventHandler handler;

    @Override
    void initView() {
        setUIContent(ResourceTable.Layout_ability_refresh);
        initAppBar();
        mMultipleStatusView = (MultipleStatusView) findComponentById(ResourceTable.Id_refresh_multiple_status_view);
        mTopImage = (Image) findComponentById(ResourceTable.Id_image1);
        handler = new EventHandler(EventRunner.getMainEventRunner());
        mMultipleStatusView.showContent();
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_recycler_view);
        mAnimator2 = mTopImage.createAnimatorProperty();
        mAnimator = mTopImage.createAnimatorProperty();
        mNewsAdapter = new EasyListProvider(this, Arrays.asList(data));
        listContainer.setItemProvider(mNewsAdapter);
        loading();
        listContainer.setTouchEventListener(this::onTouchEvent);
    }
    private void initAppBar() {
        DirectionalLayout backButton = (DirectionalLayout)
                findComponentById(ResourceTable.Id_appBar_backButton_touchTarget);
        backButton.setClickedListener(component -> onBackPressed());
    }

    //开始下拉动画
    private void startTopLoading() {
        mTopImage.setVisibility(Component.VISIBLE);
        mAnimator2.setDuration(600).setTarget(mTopImage).moveByY(100).start();
        mAnimator.setDuration(600).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).setTarget(mTopImage).setDelay(600).start();
    }

    //关闭下拉动画
    private void stopTopLoading() {

        mTopImage.setVisibility(Component.HIDE);
        mAnimator.cancel();
    }


    /**
     * 需要执行的事件
     *
     */
    private final Runnable callback = new Runnable() {
        @Override
        public void run() {
            stopTopLoading();
        }
    };
    /**
     * 延时执行任务
     *
     * @param runnable
     * @param delay
     */
    public void postDelayed(Runnable runnable, int delay) {
        handler.postTask(runnable, delay);
    }
    @Override
    public boolean onTouchEvent(Component component, TouchEvent ev) {
        final int action = ev.getAction();

        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityDetector.obtainInstance();
        }
        mVelocityTracker.addEvent(ev);
        int sumX = 0;
        int sumY = 0;
        switch (action) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                mLastX = ev.getPointerPosition(ev.getIndex()).getX();
                mLastY = ev.getPointerPosition(ev.getIndex()).getY();
                break;

            case TouchEvent.POINT_MOVE:
                float current_Y = ev.getPointerPosition(ev.getIndex()).getY();
                scrollY = current_Y - mLastY;
                refresh = true;
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                int offsetY = (int) scrollY;
                if (offsetY >10&&!listContainer.canScroll(Component.DRAG_DOWN)) {
                    startTopLoading();
                }
                postDelayed(callback, 2000);
                refresh = false;
                mLastY = 0;
                break;
            case TouchEvent.CANCEL:
                break;
        }
        return true;
    }
}
